import asyncio
import websockets
import time
import re
import requests
import sys
# 检测客户端权限，用户名密码通过才能退出循环
async def check_permit(websocket):
    while True:
        recv_str = await websocket.recv()
        cred_dict = recv_str.split(":")
        if cred_dict[0] == "admin" and cred_dict[1] == "123456":
            i = 0
            rr = cred_dict[2]
            while True:
                print(rr)
                getk_line_data_url = 'http://hq.sinajs.cn/list=' + rr
                res = requests.get(url=getk_line_data_url)
                regex = '"([^"]+)"'
                result = re.findall(regex, res.text)
                split_str = re.split(",", result[0])
                print(split_str)
                await websocket.send(str(split_str))
                time.sleep(2)
            return True
        else:
            response_str = "sorry, the username or password is wrong, please submit again"
            await websocket.send(response_str)

# 接收客户端消息并处理，这里只是简单把客户端发来的返回回去
async def recv_msg(websocket):
    while True:
        recv_text = await websocket.recv()
        response_text = f"your submit context: {recv_text}"
        await websocket.send(response_text)

# 服务器端主逻辑
async def main_logic(websocket, path):
    await check_permit(websocket)
    await recv_msg(websocket)

start_server = websockets.serve(main_logic, 'localhost', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()